/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import pojo.Message;
import pojo.Property;
import pojo.QueryInfo;

/**
 *
 * @author HoangLiem
 */
public class ExecDeleteQuery {
    
    public static boolean validateAndExecQuery(String databaseName, String query, Message message) {
        boolean result = true;
        try {
            String tableName = "";
            String originalWhere = "";//bien nay luu chinh xac thanh phan where
            String tempQuery = query;
            tempQuery = ExecQuery.trimMoreSpace(tempQuery);
            
            String[] s = tempQuery.split(" ");
            if (s.length < 5) {// phai it nhất co 5 đoan trong cau delete
                message.setMessage("Query not enough components");
                return false;
            }
            //kiem tra ton tai delete, la doan có chi so 0 trong s
            if (!s[0].toLowerCase().trim().equals("delete")) {
                message.setMessage("Error at 'delete' component");
                return false;
            }
            //kiem tra thanh phan from
            if (!s[1].toLowerCase().trim().equals("from")) {
                message.setMessage("Error at 'from' component");
                return false;
            }
            //Kiem tra ton  tại ten bang
            ArrayList<String> tableNames = ExecTableXML.getNamesOfTable(databaseName);
            if (!ExecXML.isExistedString(tableNames, s[2].toLowerCase().trim())) {
                message.setMessage("Table '" + s[2] + "' does not exists");
                return false;
            } else {
                tableName = s[2].toLowerCase().trim();
            }
            //kiem tra có thành phần where
            if (!s[3].toLowerCase().trim().equals("where")) {
                message.setMessage("Error at 'where' component");
                return false;
            } else {
                originalWhere = s[3];
            }
            //kiem tra thanh phan WHERE
            //luc nay ta ko xài chuoi tempQuery mà su dung chuoi query goc de lay thanh phan sau where.
            String where = query.substring(query.indexOf(originalWhere));
            where = where.replace(originalWhere, "").trim();
            ArrayList<String> splitOr = ExecQuery.splitOr(where);
            ArrayList<QueryInfo> queryInfoList = new ArrayList<QueryInfo>();
            //moi thong tin sau khi tach theo OR, ta luu vao 1 queryInfo
            //mot cau truy van OR co the tach thanh 2 cau truy van con
            if (splitOr.size() > 0) {
                for (int i = 0; i < splitOr.size(); i++) {
                    String or = splitOr.get(i).trim();
                    if (or.equals("")) {
                        message.setMessage("Error at codition");
                        return false;
                    } else {
                        ArrayList<String> splitAnd = ExecQuery.splitAnd(or);
                        if (splitAnd.size() > 0) {
                            ArrayList<String> propertyNames = new ArrayList<String>();
                            ArrayList<String> propertyData = new ArrayList<String>();
                            for (int j = 0; j < splitAnd.size(); j++) {
                                String and = splitAnd.get(j).trim();
                                if (and.equals("") || and.length() < 3) {
                                    message.setMessage("Error at codition");
                                    return false;
                                } else {
                                    ArrayList<String> splitEqual = ExecQuery.splitEqual(and);
                                    if (splitEqual.size() > 0) {//chia theo dấu =
                                        if (splitEqual.size() > 2) {
                                            message.setMessage("Error at condition, much '='");
                                            return false;
                                        } else {
                                            //kiem tra ton tai thuoc tinh, data
                                            String field = splitEqual.get(0).trim();
                                            String data = splitEqual.get(1).trim();
                                            if (field.equals("") || data.equals("")) {
                                                message.setMessage("Error at condition, field name or data empty");
                                                return false;
                                            }
                                            ArrayList<String> fieldNames = ExecTableXML.getAllFieldsFromTable(databaseName, tableName);
                                            if (!ExecXML.isExistedString(fieldNames, field)) {
                                                message.setMessage("Error at condition, field name not exist");
                                                return false;
                                            }
                                            Property pro = ExecTableXML.getFieldOfTable(databaseName, tableName, field);
                                            if (pro == null) {
                                                message.setMessage("Error when get info of '" + field + "'");
                                                return false;
                                            }
                                            if (pro.getType().equalsIgnoreCase("string")) {
                                                //kiem tra la chuoi
                                                if (data.length() >= 2) {
                                                    if (data.charAt(0) == '\'' && data.charAt(data.length() - 1) == '\'') {
                                                        data = ExecQuery.trimCharsFirstEnd(data);
                                                        if (!data.contains("'")) {
                                                            if (data.length() > pro.getLength()) {
                                                                message.setMessage("Error data at '" + data + "', length data greater length in field");
                                                                return false;
                                                            }
                                                            propertyNames.add(field);
                                                            propertyData.add(data);
                                                        } else {
                                                            message.setMessage("Error data ' at '" + data + "'");
                                                            return false;
                                                        }
                                                        
                                                    } else {
                                                        message.setMessage("Error data ' at '" + data + "'");
                                                        return false;
                                                    }
                                                } else {
                                                    message.setMessage("Error data at '" + data + "'");
                                                    return false;
                                                }
                                            } else {
                                                //kiem tra la so nguyen
                                                if (pro.getType().equalsIgnoreCase("integer")) {
                                                    if (ExecXML.isInteger(data)) {
                                                        propertyNames.add(field);
                                                        propertyData.add(data);
                                                    } else {
                                                        message.setMessage("Error data at '" + data + "'");
                                                        return false;
                                                    }
                                                } else {//kiem tra so thuc
                                                    if (ExecXML.isReal(data)) {
                                                        propertyNames.add(field);
                                                        propertyData.add(data);
                                                    } else {
                                                        message.setMessage("Error data at '" + data + "'");
                                                        return false;
                                                    }
                                                }
                                            }
                                            
                                        }
                                    } else {
                                        message.setMessage("Error at codition");
                                        return false;
                                    }
                                }
                            }
                            //Add thong tin vao queryInfo
                            QueryInfo queryInfo = new QueryInfo(propertyNames, propertyData);
                            queryInfoList.add(queryInfo);
                        } else {
                            message.setMessage("Error at codition");
                            return false;
                        }
                    }
                }
            }
            //Sau khi thanh con cac buoc tren, ta bat dau hanh dong delte
            if (!queryInfoList.isEmpty()) {
                int deleted = 0;
                for (int i = 0; i < queryInfoList.size(); i++) {
                    QueryInfo queryInfo = queryInfoList.get(i);
                    int n = ExecRowXML.deleteRow(databaseName, tableName, queryInfo);
                    deleted = deleted + n;
                }
                message.setMessage(deleted + " rows deleted");
            }
        } catch (Exception ex) {
            message.setMessage("Error when delete");
            result = false;
        }
        return result;
    }
}
